import 'dart:convert';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_plugin_eth_wallet/models/result.dart';

class ContractUtils {
  static const MethodChannel _channel = const MethodChannel('contractUtils');

  static const String _methodType = "contract";

  /// 合约配置
  ///
  /// url:部署合约的服务器地址
  /// contractAddress:合约地址
  /// contractAddress:合约地址
  /// tokenName:代币名称
  static Future config(
      String url, String contractAddress, String tokenName) async {
    await _channel.invokeMethod<dynamic>(
        '$_methodType.config', <String, dynamic>{
      'url': url,
      'contractAddress': contractAddress,
      'tokenName': tokenName
    });
  }

  /// 合约转出
  ///
  /// toAddress:转出地址
  /// value:转出数量
  static Future<Result> transfer(
      String password, String toAddress, double value) async {
    var resString = await _channel.invokeMethod<dynamic>(
        '$_methodType.transfer', <String, dynamic>{
      'password': password,
      'toAddress': toAddress,
      'value': value
    });

    return new Result(json.decode(resString));
  }

  /// 合约余额
  ///
  /// address:余额地址
  static Future<String> balance() async {
    return await _channel
        .invokeMethod<dynamic>('$_methodType.balance', <String, dynamic>{});
  }
}
